/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.WorkStateEnum;
import com.ykht.proj.coupon.entity.FxWaiterExt;
import com.ykht.proj.coupon.mapper.FxWaiterExtMapper;
import com.ykht.proj.coupon.service.FxWaiterExtService;
import com.ykht.proj.coupon.vo.FxWaiterExtVo;
import com.ykht.proj.coupon.vo.FxWithdrawalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务员扩展表
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:18
 */
@Service
public class FxWaiterExtServiceImpl extends ServiceImpl<FxWaiterExtMapper, FxWaiterExt> implements FxWaiterExtService {
	/**
	 * 服务员扩展表  Mapper 层
	 */
	@Autowired
	private FxWaiterExtMapper fxWaiterExtMapper;

	/**
	 * 分销分页列表
	 *
	 * @param page
	 * @param fxWaiterExtVo
	 * @return
	 */
	@Override
	public IPage<FxWaiterExtVo> findPage(Page page, FxWaiterExtVo fxWaiterExtVo) {
		return fxWaiterExtMapper.findPage(page, fxWaiterExtVo);
	}

	/**
	 * 记录详情的饭友 分页查询（饭友概况）统计
	 *
	 * @param fxWaiterExtVo
	 * @return
	 */
	@Override
	public FxWaiterExtVo getFanYouListPageCount(FxWaiterExtVo fxWaiterExtVo) {
		return fxWaiterExtMapper.getFanYouListPageCount(fxWaiterExtVo);
	}

	/**
	 * 记录详情的饭友 分页查询
	 *
	 * @param page
	 * @param fxWaiterExtVo
	 * @return
	 */
	@Override
	public IPage<FxWaiterExtVo> getFanYouListPage(Page page, FxWaiterExtVo fxWaiterExtVo) {
		return fxWaiterExtMapper.getFanYouListPage(page, fxWaiterExtVo);
	}

	@Override
	public FxWaiterExt findByLoginId(Long userId) {
		return this.baseMapper.findByLoginId(userId);
	}

	@Override
	public R replaceShop(FxWaiterExt fxWaiterExt) {
		Long userId = SecurityUtils.getUserId();
		Long id = null;
		fxWaiterExt.setLoginId(userId);
		fxWaiterExt.setStartTime(LocalDateTime.now());
		fxWaiterExt.setCreateUser(userId);
		fxWaiterExt.setCreateTime(LocalDateTime.now());
		fxWaiterExt.setDelFlag(0);
		// 获取现在的门店
		FxWaiterExt waiterExt = this.getOne(Wrappers.lambdaQuery(FxWaiterExt.class)
				.eq(FxWaiterExt::getLoginId, userId)
				.eq(FxWaiterExt::getWorkState, 1).eq(FxWaiterExt::getDelFlag, 0));
		// 判断现在是否有门店
		if (null != waiterExt) {
			if (waiterExt.getShopId().equals(fxWaiterExt.getShopId())) {
				return R.failed("你已在当前门店");
			}
			waiterExt.setEndTime(LocalDateTime.now());
			waiterExt.setUpdateTime(LocalDateTime.now());
			waiterExt.setUpdateUser(userId);
			// 判断是否是同一品牌
			if (fxWaiterExt.getBrandId().equals(waiterExt.getBrandId())) {
				// 把现在的门店工作状态 改为上家工作（上家工作的商家）
				waiterExt.setWorkState(WorkStateEnum.WORK_STATE_2.getValue());
				this.updateById(waiterExt);
				//  添加现在的服务信息
				fxWaiterExt.setParentId(waiterExt.getId());
				fxWaiterExt.setWorkState(WorkStateEnum.WORK_STATE_1.getValue());
				this.save(fxWaiterExt);
				id = fxWaiterExt.getId();
			} else {
				// 获取之前品牌的所以门店服务员ID 并修改他们状态为 上家工作的品牌
				List<Long> waiterIdList = getWaiterIdList();
				// 把之前品牌的所以服务员信息修改状态为 上家工作的品牌 ，在添加新的服务员数据
				waiterExt.setWorkState(WorkStateEnum.WORK_STATE_3.getValue());
				this.updateById(waiterExt);
				UpdateWrapper<FxWaiterExt> updateWrapper = new UpdateWrapper<>();
				updateWrapper.lambda().in(FxWaiterExt::getId, waiterIdList)
						.set(FxWaiterExt::getWorkState, WorkStateEnum.WORK_STATE_3.getValue())
						.set(FxWaiterExt::getUpdateTime, LocalDateTime.now())
						.set(FxWaiterExt::getUpdateUser, userId);
				this.update(updateWrapper);
				fxWaiterExt.setWorkState(WorkStateEnum.WORK_STATE_1.getValue());
				fxWaiterExt.setParentId(0L);
				this.save(fxWaiterExt);
				id = fxWaiterExt.getId();
			}
		} else {
			fxWaiterExt.setWorkState(WorkStateEnum.WORK_STATE_1.getValue());
			fxWaiterExt.setParentId(0L);
			this.save(fxWaiterExt);
			id = fxWaiterExt.getId();
		}
		return R.ok(id);
	}

	/**
	 * 获取当前登录人 服务员ID集合
	 *
	 * @return
	 */
	@Override
	public List<Long> getWaiterIdList() {
		//  获取现在的服务员信息
		FxWaiterExt waiterExt = this.getOne(Wrappers.lambdaQuery(FxWaiterExt.class)
				.eq(FxWaiterExt::getLoginId, SecurityUtils.getUserId())
				.eq(FxWaiterExt::getWorkState, WorkStateEnum.WORK_STATE_1.getValue()).eq(FxWaiterExt::getDelFlag, 0));
		List<FxWaiterExt> waiterExtList = this.list(Wrappers.lambdaQuery(FxWaiterExt.class)
				.eq(FxWaiterExt::getLoginId, SecurityUtils.getUserId())
				.in(FxWaiterExt::getWorkState, WorkStateEnum.WORK_STATE_1.getValue(),
						WorkStateEnum.WORK_STATE_2.getValue())
				.eq(FxWaiterExt::getBrandId, waiterExt.getBrandId()).eq(FxWaiterExt::getDelFlag, 0));
		List<Long> waiterIdList = waiterExtList.stream().map(FxWaiterExt::getId).collect(Collectors.toList());
		return waiterIdList;
	}

	/**
	 * 获取服务员列表
	 *
	 * @param page
	 * @param fxWaiterExtVo
	 * @return
	 */
	@Override
	public IPage<FxWaiterExtVo> getWaiterExtPageList(Page page, FxWaiterExtVo fxWaiterExtVo) {
		return fxWaiterExtMapper.getWaiterExtPageList(page, fxWaiterExtVo);
	}
}
